#include "SDMonitorThread.h"

CSDMonitorThread::CSDMonitorThread(void)
{
	m_hDestWnd		=	NULL;
	m_bStop =  FALSE;
}

CSDMonitorThread::CSDMonitorThread(HWND hDestWnd)
{
	m_hDestWnd		=	hDestWnd;
	m_bStop =  FALSE;
}

CSDMonitorThread::~CSDMonitorThread(void)
{

}

void CSDMonitorThread::Stop()
{
	m_bStop = TRUE;
}

void CSDMonitorThread::Run()
{
	BOOL bSdIn = TRUE;


	if(!m_hDestWnd)
	{
		return;
	}

	//Monitor the storage card insert or remove 

#ifndef UNDER_CE
		return;
#endif

		Sleep(1500);

		if(INVALID_FILE_ATTRIBUTES == GetFileAttributes(CUtil::GetStorageCardName().c_str()))
		{
			bSdIn = FALSE;
		}
		else
		{
			bSdIn = TRUE;
			PostMessage(m_hDestWnd,WM_SDNOTIFY,SDIN,0);
		}

		while(!m_bStop)
		{
			if(INVALID_FILE_ATTRIBUTES == GetFileAttributes(CUtil::GetStorageCardName().c_str()))
			{
				if(bSdIn) //remove
				{
					bSdIn = FALSE;
					// notify the main window
					
					Sleep(500);
					PostMessage(m_hDestWnd,WM_SDNOTIFY,SDOUT,0);
				//	PlaySound(L"\\Windows\\windmin.wav", NULL, SND_FILENAME|SND_ASYNC|SND_NODEFAULT);		
				}
				else
				{
					//do nothing
				}
			}
			else
			{
				if(!bSdIn) //insert 
				{
					bSdIn = TRUE;
					// notify the main window
					Sleep(500);
					PostMessage(m_hDestWnd,WM_SDNOTIFY,SDIN,0);
			//		PlaySound(L"\\Windows\\windmax.wav", NULL, SND_FILENAME|SND_ASYNC|SND_NODEFAULT);	
				}
				else
				{
					//do nothing
				}
			}

			Sleep(500);
		}
}
